package org.iabako.server.controller.timer.demo;

import org.apache.log4j.Logger;
import org.iabako.shared.entity.business.Address;
import org.iabako.shared.entity.business.Client;
import org.iabako.shared.entity.enumeration.Country;
import org.iabako.shared.entity.enumeration.Gender;
import org.iabako.shared.entity.user.Enterprise;
import org.iabako.shared.exception.IabakoActionForbiddenException;
import org.iabako.shared.iservice.business.ClientService;
import org.iabako.shared.tools.GenericTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * Created by jose on 07/01/15.
 */

@Controller
public class CreateClientController {

    static Logger log = Logger.getLogger(CreateClientController.class.getName());

    @Autowired
    ClientService clientService;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void create(Enterprise enterprise, String csvFileClients) throws IOException, URISyntaxException {
        URL csvFileURL = this.getClass().getClassLoader().getResource(csvFileClients);
        BufferedReader br = new BufferedReader(new FileReader(new File(csvFileURL.toURI())));
        String line = "";

        int index = 0;

        while ((line = br.readLine()) != null) {
            if (index++ == 0) {
                //skip header
                continue;
            }

            String[] token = line.split(";");
            if (token.length == 10) {

                String lastName = token[2].substring(token[2].lastIndexOf(" "));
                String firstName = token[2].substring(0, token[2].indexOf(lastName));

                Client client = new Client();
                client.setEnterprise(enterprise);
                client.setNumber(token[0]);
                client.setGender(Gender.valueOf(token[1]));
                client.setFirstName(firstName);
                client.setLastName(lastName);
                //stupid name calculation
                client.getName();

                client.setEmail(token[3]);
                client.setBirthday(GenericTools.getDate(token[4]));

                Address address = new Address();
                address.setCountry(Country.valueOf(token[5]));
                address.setAddressLine1(token[6]);
                address.setCity(token[7]);
                address.setZipCode(token[8]);

                client.setAddress(address);
                client.setPhoneNumber1(token[9]);

                try {
                    clientService.save(client);
                } catch (IabakoActionForbiddenException e) {
                    log.error(e.getMessage(), e);
                    //Should never happen
                }
            }
        }
    }
}
